//
//  MianViewController.swift
//  ShangHaiWeiBo
//
//  Created by 蔡杰 on 16/2/22.
//  Copyright © 2016年 蔡杰. All rights reserved.
//

import UIKit

class MianViewController: UITabBarController {
    //测试推送

    override func viewDidLoad() {
        super.viewDidLoad()

        addchildViewControllers()
        
        
        setTabber()
        showNewFeature()
        print("nihao")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //设置到顶部
        tabBar.bringSubviewToFront(comporButten)
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        comporButten.frame = CGRectInset(tabBar.bounds, width*2, 0)
        
    }
    
    private lazy var comporButten:UIButton = {
    
        //创建控件
        let button = UIButton()
        
        let buttonName = "tabbar_compose_icon_add"
        //设置控件的背景图片
        button.setImage(UIImage(named: buttonName), forState: .Normal)
        button.setImage(UIImage(named: buttonName + "_highlighted"), forState: .Highlighted)
        
        let backImageName = "tabbar_compose_button"
        button.setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        button.setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        
        //使控件根据图片调整大小
        button.sizeToFit()
        
        return button
     
    }()
    

     @objc private func clickComposeButton(){
    
        print("点到了")
    }
 


}
// MARK: - 新特性的处理
extension MianViewController {

    private func showNewFeature (){
    //判断用户是否登陆，没有的话直接返回。它会自动跳转到oauth界面
        if !UserAccount.sharedUserAccount.isLogin{
        
            return
        }
        //判断是否有更新
        let v = isNewVersion ? NewFeatureView():WelcomeView()
        
        view.addSubview(v)
    }
    
    //判断是否有新版本
    private var isNewVersion: Bool {
    
        //或得当前版本
        let currenVersion = NSBundle.cj_curentVersion
    
        //获得之前版本号
        let versionKey = "cn.itcast.versionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        //将当前版本保存到偏好设置中
        NSUserDefaults.standardUserDefaults().setObject(currenVersion, forKey: versionKey)
        return currenVersion != sandboxVersion
        
    }
}

// MARK: - 添加控制器和添加子控件
extension  MianViewController {
    private func setTabber(){
        
        tabBar.shadowImage = UIImage.cz_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        tabBar.addSubview(comporButten)
        
        comporButten.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
        
        
    }
    private func addchildViewControllers() {
        
        addChildViewController(HomeViewController(), vcNameImage: "tabbar_home", vcTitle: "首页")
        addChildViewController(MessageViewController(), vcNameImage: "tabbar_message_center", vcTitle: "消息")
        //添加一个空白的控制器使中间空出个位置用了添加“+”
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), vcNameImage: "tabbar_discover", vcTitle: "发现")
        addChildViewController(ProfileViewController(), vcNameImage: "tabbar_profile", vcTitle: "我")
        //
    }

    private func addChildViewController(vc: UIViewController, vcNameImage: String ,vcTitle: String) {
        //设置控制器的命名
        vc.title = vcTitle
        //        vc.navigationItem.title = vcTitle
        //设置
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor() ], forState: .Selected)
        //设置tabbar栏按键的背景图片
        vc.tabBarItem.image = UIImage(named: vcNameImage)
        vc.tabBarItem.selectedImage = UIImage(named: vcNameImage + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        let nav = UINavigationController(rootViewController: vc)
        
        
        
        addChildViewController(nav)
        
    }

    

    
}